package com.akjava.lib.android.map;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MapIO {

	public static final int[][] loadIntMapData(InputStream input) throws  IOException{
		BufferedReader reader=new BufferedReader(new InputStreamReader(input));
		String line;
		ArrayList<int[]> list=new ArrayList<int[]>();
		while((line=reader.readLine())!=null){
			if(line.equals("")){
				//ignore
			}else{
				String[] vs=line.split(",");
				int ints[]=new int[vs.length];
				for (int i = 0; i < ints.length; i++) {
					ints[i]=Integer.parseInt(vs[i]);
				}
				list.add(ints);
			}
			
		}
		int[][] map=new int[list.size()][];
		for (int i = 0; i < map.length; i++) {
			map[i]=list.get(i);
		}
		return map;
	}
	
public static final void writInteMapData(int[][] map,File file) throws IOException{
		StringBuffer output=new StringBuffer();
		for(int i=0;i<map.length;i++){
		
			for(int j=0;j<map[i].length;j++){
				output.append(Integer.toString(map[i][j]));
				if(j!=map[i].length-1){
					output.append(",");
				}
			}
			
			if(i!=map.length-1){
				output.append("\r\n");
			}
		}
		
		
			BufferedWriter writer=new BufferedWriter(new FileWriter(file));
			writer.write(output.toString());
			writer.close();
		
		
	}
}
